home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / os2 / plnk081.zip / pilot-link.0.8.1 / libsock / expense.c < prev    next >
C/C++ Source or Header  |  1997-08-08  |  7KB  |  320 lines

  1. /* expense.c:  Translate Pilot expense tracker data formats
  2.  *
  3.  * Copyright (c) 1997, Kenneth Albanowski
  4.  *
  5.  * This is free software, licensed under the GNU Public License V2.
  6.  * See the file COPYING for details.
  7.  */
  8.  
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include "pi-source.h"
  13. #include "pi-socket.h"
  14. #include "pi-dlp.h"
  15. #include "pi-expense.h"
  16.  
  17. char * ExpenseSortNames[] =
  18.     { "Date", "Type", NULL };
  19. char * ExpenseDistanceNames[] =
  20.     { "Miles", "Kilometers", NULL };
  21. char * ExpensePaymentNames[] =
  22.     { "AmEx", "Cash", "Check", "CreditCard", "MasterCard", "Prepaid", "VISA", "Unfiled" };
  23. char * ExpenseTypeNames[] =
  24.     { "Airfare", "Breakfast", "Bus", "Business Meals", "Car Rental", "Dinner",
  25.     "Entertainment", "Fax", "Gas", "Gifts", "Hotel", "Incidentals", "Laundry",
  26.     "Limo", "Lodging", "Lunch", "Mileage", "Other", "Parking", "Postage",
  27.     "Snack", "Subway", "Supplies", "Taxi", "Telephone", "Tips", "Tolls",
  28.     "Train"
  29. };
  30.  
  31. void free_Expense(struct Expense * a) {
  32.   if (a->note)
  33.     free(a->note);
  34.   if (a->amount)
  35.     free(a->amount);
  36.   if (a->city)
  37.     free(a->city);
  38.   if (a->vendor)
  39.     free(a->vendor);
  40.   if (a->attendees)
  41.     free(a->attendees);
  42. }
  43.  
  44. int unpack_Expense(struct Expense * a, unsigned char * buffer, int len)
  45. {
  46.   unsigned long d;
  47.   unsigned char * start = buffer;
  48.   
  49.   if (len<6)
  50.     return 0;
  51.                                                                                                                                                                                                             
  52.   d = (unsigned short int)get_short(buffer);
  53.   a->date.tm_year = (d >> 9) + 4;
  54.   a->date.tm_mon = ((d >> 5) & 15) - 1;
  55.   a->date.tm_mday = d & 31;
  56.   a->date.tm_hour = 0;
  57.   a->date.tm_min = 0;
  58.   a->date.tm_sec = 0;
  59.   a->date.tm_isdst = -1;
  60.   mktime(&a->date);
  61.   
  62.   a->type = get_byte(buffer+2);
  63.   a->payment = get_byte(buffer+3);
  64.   a->currency = get_byte(buffer+4);
  65.   
  66.   buffer += 6;
  67.   len -= 6;
  68.   
  69.   if (len<1)
  70.     return 0;
  71.   
  72.   if (*buffer) {
  73.     a->amount = strdup(buffer);
  74.     buffer += strlen(a->amount);
  75.     len -= strlen(a->amount);
  76.   } else {
  77.     a->amount = 0;
  78.   }
  79.   buffer++;
  80.   len--;
  81.   
  82.   if (len<1)
  83.     return 0;
  84.  
  85.   if (*buffer) {
  86.     a->vendor = strdup(buffer);
  87.     buffer += strlen(a->vendor);
  88.     len -= strlen(a->vendor);
  89.   } else {
  90.     a->vendor = 0;
  91.   }
  92.   buffer++;
  93.   len--;
  94.  
  95.   if (len<1)
  96.     return 0;
  97.  
  98.   if (*buffer) {
  99.     a->city = strdup(buffer);
  100.     buffer += strlen(a->city);
  101.     len -= strlen(a->city);
  102.   } else {
  103.     a->city = 0;
  104.   }
  105.   buffer++;
  106.   len--;
  107.  
  108.   if (len<1)
  109.     return 0;
  110.  
  111.   if (*buffer) {
  112.     a->attendees = strdup(buffer);
  113.     buffer += strlen(a->attendees);
  114.     len -= strlen(a->attendees);
  115.   } else {
  116.     a->attendees = 0;
  117.   }
  118.   buffer++;
  119.   len--;
  120.  
  121.   if (len<1)
  122.     return 0;
  123.  
  124.   if (*buffer) {
  125.     a->note = strdup(buffer);
  126.     buffer += strlen(a->note);
  127.     len -= strlen(a->note);
  128.   } else {
  129.     a->note = 0;
  130.   }
  131.  
  132.   buffer++;
  133.   len--;
  134.   
  135.   return (buffer - start);
  136. }
  137.  
  138. int pack_Expense(struct Expense * a, unsigned char * record, int len)
  139. {
  140.   unsigned char * buf = record;
  141.   int destlen = 6+1+1+1+1+1;
  142.   
  143.   if (a->amount)
  144.     destlen += strlen(a->amount);
  145.   if (a->vendor)
  146.     destlen += strlen(a->vendor);
  147.   if (a->city)
  148.     destlen += strlen(a->city);
  149.   if (a->attendees)
  150.     destlen += strlen(a->attendees);
  151.   if (a->note)
  152.     destlen += strlen(a->note);
  153.   
  154.   if (!record)
  155.     return destlen;
  156.   if (len<destlen)
  157.     return 0;
  158.   
  159.   set_short(buf, ((a->date.tm_year - 4) << 9) |
  160.                    ((a->date.tm_mon  + 1) << 5) |
  161.                    a->date.tm_mday);
  162.   buf += 2;
  163.   set_byte(buf, a->type);
  164.   set_byte(buf+1, a->payment);
  165.   set_byte(buf+2, a->currency);
  166.   set_byte(buf+3, 0); /*gapfil*/ 
  167.   buf += 4;
  168.   
  169.   if (a->amount) {
  170.     strcpy(buf, a->amount);
  171.     buf += strlen(buf);
  172.   } else {
  173.     set_byte(buf, 0);
  174.   }
  175.   buf++;
  176.  
  177.   if (a->vendor) {
  178.     strcpy(buf, a->vendor);
  179.     buf += strlen(buf);
  180.   } else {
  181.     set_byte(buf, 0);
  182.   }
  183.   buf++;
  184.   
  185.   if (a->city) {
  186.     strcpy(buf, a->city);
  187.     buf += strlen(buf);
  188.   } else {
  189.     set_byte(buf, 0);
  190.   }
  191.   buf++;
  192.   
  193.   if (a->attendees) {
  194.     strcpy(buf, a->attendees);
  195.     buf += strlen(buf);
  196.   } else {
  197.     set_byte(buf, 0);
  198.   }
  199.   buf++;
  200.   
  201.   if (a->note) {
  202.     strcpy(buf, a->note);
  203.     buf += strlen(buf);
  204.   } else {
  205.     set_byte(buf, 0);
  206.   }
  207.   buf++;
  208.   
  209.   return (buf-record);
  210. }
  211.  
  212. int unpack_ExpenseAppInfo(struct ExpenseAppInfo * ai, unsigned char * record, int len)
  213. {
  214.   int i;
  215.   unsigned char*start = record;
  216.   i = unpack_CategoryAppInfo(&ai->category, record, len);
  217.   if (!i)
  218.     return 0;
  219.   record+=i;
  220.   len-=i;
  221.   if (len < 2+(16+4+8)*4);
  222.   ai->sortOrder = get_byte(record);
  223.   record += 2;
  224.   for(i=0;i<4;i++) {
  225.     memcpy(ai->currencies[i].name, record, 16);
  226.     record+=16;
  227.     memcpy(ai->currencies[i].symbol, record, 4);
  228.     record+=4;
  229.     memcpy(ai->currencies[i].rate, record, 8);
  230.     record+=8;
  231.   }
  232.   return (record-start);
  233. }
  234.  
  235. int pack_ExpenseAppInfo(struct ExpenseAppInfo * ai, unsigned char * record, int len)
  236. {
  237.   unsigned char * start = record;
  238.   int i;
  239.   int destlen = 2+(16+4+8)*4;
  240.   
  241.   i = pack_CategoryAppInfo(&ai->category, record, len);
  242.   if (!record)
  243.     return i + destlen;
  244.   if (!i)
  245.     return i;
  246.   record += i;
  247.   len -= i;
  248.   if (len < destlen)
  249.     return 0;
  250.   set_byte(record, ai->sortOrder);
  251.   set_byte(record+1, 0); /* gapfil */
  252.   record += 2;
  253.   for(i=0;i<4;i++) {
  254.     memcpy(record, ai->currencies[i].name, 16);
  255.     record+=16;
  256.     memcpy(record, ai->currencies[i].symbol, 4);
  257.     record+=4;
  258.     memcpy(record, ai->currencies[i].rate, 8);
  259.     record+=8;
  260.   }
  261.  
  262.   return (record-start);
  263. }
  264.  
  265. int unpack_ExpensePref(struct ExpensePref * p, unsigned char * record, int len)
  266. {
  267.   int i;
  268.   unsigned char * start = record;
  269.   p->currentCategory = get_short(record);
  270.   record += 2;
  271.   p->defaultCategory = get_short(record);
  272.   record += 2;
  273.   p->noteFont = get_byte(record);
  274.   record++;
  275.   p->showAllCategories = get_byte(record);
  276.   record++;
  277.   p->showCurrency = get_byte(record);
  278.   record++;
  279.   p->saveBackup = get_byte(record);
  280.   record++;
  281.   p->allowQuickFill = get_byte(record);
  282.   record++;
  283.   p->unitOfDistance = get_byte(record);
  284.   record += 2;
  285.   for(i=0;i<7;i++) {
  286.     p->currencies[i] = get_byte(record);
  287.     record++;
  288.   }
  289.   return (record - start);
  290. }
  291.  
  292. int pack_ExpensePref(struct ExpensePref * p, unsigned char * record, int len)
  293. {
  294.   int i;
  295.   unsigned char * start = record;
  296.   set_short(record, p->currentCategory);
  297.   record += 2;
  298.   set_short(record, p->defaultCategory);
  299.   record += 2;
  300.   set_byte(record, p->noteFont);
  301.   record++;
  302.   set_short(record, p->showAllCategories);
  303.   record++;
  304.   set_byte(record, p->showCurrency);
  305.   record++;
  306.   set_byte(record, p->saveBackup);
  307.   record++;
  308.   set_byte(record, p->allowQuickFill);
  309.   record++;
  310.   set_byte(record, p->unitOfDistance);
  311.   record++;
  312.   set_byte(record, 0); /* gapfil ?? */
  313.   record++;
  314.   for(i=0;i<7;i++) {
  315.     set_byte(record, p->currencies[i]);
  316.     record++;
  317.   }
  318.   return record-start;
  319. }
  320.